//
//  HWXiaoHaotrumpetInterfaceViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/11/17.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWXiaoHaotrumpetInterfaceViewController.h"
#import "HWXiaoHaoGroupModel.h"
#import "HWxiaohaofenzutakuangView.h"
#import "HWXihaolibaoTableViewCell.h"


@interface HWXiaoHaotrumpetInterfaceViewController ()<UITableViewDataSource,UITableViewDelegate,HWxiaohaofenzutakuangViewDelegate,HWXihaolibaoTableViewCellDelegate>
@property(strong,nonatomic)UITableView *mainaTableVie;
@property(strong,nonatomic)NSMutableArray *dataMuArr;
@property(assign,nonatomic) int xuanzhong;





@end

@implementation HWXiaoHaotrumpetInterfaceViewController

-(UITableView *)mainaTableVie {

    if (_mainaTableVie == nil
        ) {

         _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,kScreenH) style:UITableViewStylePlain];
         _mainaTableVie.delegate = self;
         _mainaTableVie.dataSource = self;
         _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
         _mainaTableVie.backgroundColor = [UIColor whiteColor];
         _mainaTableVie.sectionHeaderHeight = 44;
        

    }
    return   _mainaTableVie ;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
    
    [self fasongwangluoqingqiu];
    

    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)setupUI {
    
    
    self.view.backgroundColor = [UIColor redColor];
     self.navigationItem.title = @"小号管理";
    
    self.navigationItem.title = [HWjianzhiduiObject keylocalization:@"小号管理"];
    
    [self.view  addSubview:self.mainaTableVie];
    
    
    
    
    
}


- (void)fasongwangluoqingqiu {
    
    
    
    
        NSString *userID = [HWLocallyStoredData getLoginUserID];
        NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
        NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
        NSString *signMD5 = [WMUtil md5HexDigest:sign];
        NSString *typeStr =  @"1";
        NSDictionary *parametes = @{
                                            @"ct" :  @"app",
                                            @"ac" : @"small_search",
                                            @"ios" : @"1",
                                            @"uid": userID,
                                            @"t" :tStr,
                                            @"sign": signMD5,
                                            @"Version":@"3.0",
                                            @"member_id":[HWLocallyStoredData getCode],
            
                                            };
        
  
        [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
            
            // [SVProgressHUD dismiss];
       
            
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

            if ([codeStr intValue] == 1) {
                
                self.dataMuArr = [HWXiaoHaoGroupModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
    
                [self.mainaTableVie reloadData];
                 
               
            } else {
                
                //    [self.mainaTableVie reloadData];
                
            }
            
            
            
            NSLog(@"%@",response);
            
            
        } failure:^(NSString *error) {
            
            
            
            
            
        }];
        
    
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
  
    
    
    
    return  self.dataMuArr.count;
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    
    
  //   return  10;
    
    
    
     HWXiaoHaoGroupModel *group = self.dataMuArr[section];
    
  //     return group.small.count;

    if (group.visible) {
          
        
        return group.small.count;


    }else{
           return 0;
    }
       
    
  
  
    // return  self.dataMuArr;
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    
    static NSString *cellID = @"cellID";
    
    
    HWXihaolibaoTableViewCell   *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:@"HWXihaolibaoTableViewCell" owner:nil options:nil].firstObject;
        
    }

    HWXiaoHaoGroupModel *group = self.dataMuArr[indexPath.section];
    HWxiaohaodanyuanModel *model1 = group.small[indexPath.row];
    cell.model = model1;
    cell.delegate = self;
    
    
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    
    
    
  
     if ((group.small.count-1)  == indexPath.row) {
         
         cell.xiabianView.hidden = YES;
         
     } else {
         
         cell.xiabianView.hidden = NO;
         
         
     }

    return cell;
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    

     HWXiaoHaoGroupModel *group = self.dataMuArr[indexPath.section];
    if ((group.small.count-1)  == indexPath.row) {
        
        return 65+10;
        
    }
    
    
    
    return  65+2;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
//   HWXiaoGroupModel  *group = self.dataMuArr[section];
//   HWXiaoHaoCustomHeadFooterView *view = [HWXiaoHaoCustomHeadFooterView headFooterViewWithTableview:tableView];
//    view.delegate = self;
//    view.group = group;
//    view.tag = section;
   // view.backgroundColor = [UIColor redColor];
    //return 返回之前headerFooterView的frame是0,所以需要在某个地方设置headerFooterView的frame
    
    // jiantouzuobian
    
    HWXiaoHaoGroupModel *group = self.dataMuArr[section];
    HWxiaohaofenzutakuangView  *view = [HWxiaohaofenzutakuangView headFooterViewWithTableview:tableView];
    view.delegate = self;
    view.dataModel = group;
  
   //  UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    

    
    return view;
    //return 返回之后，uitableview在用headerFooterView的时候就会设置headerFooterView的frame
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    return  94;
    
    
    
}

- (void)HWxiaohaofenzutakuangViewdianj:(HWXiaoHaoGroupModel *)mode1 {
    
    
    if (mode1.visible == YES) {
           mode1.visible = YES;
    } else {

         mode1.visible = NO;

    }
    

    
    [self.mainaTableVie reloadData];
    
    
    
    
    
    
}




// 删除小号
- (void)shangchuxiaohao:(HWxiaohaodanyuanModel *)model {
    
    
    
//    if ([model.is_login isEqualToString:@"1"]) {
//
//        [MBProgressHUD showError:@"主账号不能删除"];
//        return;
//
//
//    }
//
//    [MBProgressHUD show:@"正在删除"];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定删除此角色所在的小号吗" message:@"确定删除此角色所在的小号吗" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *userID = [HWLocallyStoredData getLoginUserID];
        NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
        NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
        NSString *signMD5 = [WMUtil md5HexDigest:sign];
        NSString *typeStr =  @"1";
        NSDictionary *parametes = @{
            @"ct" :  @"app",
            @"ac" : @"small_del",
            @"ios" : @"1",
            @"uid": userID,
            @"t" :tStr,
            @"sign": signMD5,
            @"small_uid":model.uid,
            @"member_id":[HWLocallyStoredData getCode],
            
        };
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
            
            // [SVProgressHUD dismiss];
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
            
            [MBProgressHUD hideHUD];
            
            if ([codeStr intValue] == 1) {
                
                [self fasongwangluoqingqiu];
                
            } else {
                //    [self.mainaTableVie reloadData];
                
                [MBProgressHUD showError:response[@"message"]];
            }
            NSLog(@"%@",response);
            
            
        } failure:^(NSString *error) {

        }];
    }];
         
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:^{
            
    }];
    
    
    
    
     
    
    
    
    
    
}


// 设置为主账号
- (void)shezhiweizhuzhanghao:(HWxiaohaodanyuanModel *)model {
    
    
        
    
         NSString *userID = [HWLocallyStoredData getLoginUserID];
          NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
          NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
          NSString *signMD5 = [WMUtil md5HexDigest:sign];
          NSString *typeStr =  @"1";
          NSDictionary *parametes = @{
                                              @"ct" :  @"app",
                                              @"ac" : @"small_login",
                                              @"ios" : @"1",
                                              @"uid": userID,
                                              @"t" :tStr,
                                              @"sign": signMD5,
                                              @"Version":@"3.0",
                                              @"id":model.id,
                                              @"member_id":[HWLocallyStoredData getCode],
              
                                              };
          
        
    
          [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
              
              // [SVProgressHUD dismiss];
         
              
              NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

              if ([codeStr intValue] == 1) {
                  
                  [self fasongwangluoqingqiu];
                  
                   
                 
              } else {
                  
                  //    [self.mainaTableVie reloadData];
                  
              }
              
              
              
              NSLog(@"%@",response);
              
              
          } failure:^(NSString *error) {
              
              
              
              
              
          }];
    
    
    
    
    
}







@end
